package codes.poiuy.utils;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Formatter;

import android.content.Context;
import android.net.ConnectivityManager;

public class Utils {

	public static ArrayList<String> encriptContactsNumbers() {

		ArrayList<String> contacts = null;

		return contacts;

	}

	public static ArrayList<String> getContacts() {

		return null;

	}

	/**
	 * Does the encryptation of password.
	 * 
	 * @param appKey
	 * @param pass
	 * @return String
	 */
	public static String encrypt(String appKey, String pass) {
		/** Defines the codification to encrypt. */
		final String CODIFICATION = "SHA1";
		String result = null;
		MessageDigest sha1;
		byte[] resultByte;
		try {
			sha1 = MessageDigest.getInstance(CODIFICATION);
			sha1.reset();
			sha1.update(pass.getBytes(Charset.defaultCharset()));
			resultByte = sha1.digest(appKey.getBytes(Charset.defaultCharset()));
			result = byteArray2Hex(resultByte);
		} catch (NoSuchAlgorithmException e) {
			sha1 = null;
		}
		return result;
	}

	/**
	 * Converts a byte array to hex base.
	 * 
	 * @param hash
	 * @return String
	 */
	private static String byteArray2Hex(byte[] hash) {
		Formatter formatter = new Formatter();
		for (byte b : hash) {
			formatter.format("%02x", b);
		}
		return formatter.toString();
	}

	/**
	 * Method to check if the net connection in.
	 * 
	 * @param context
	 * @return boolean
	 */
	public static boolean isInternetConnected(Context context) {
		final ConnectivityManager connMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		final android.net.NetworkInfo wifi = connMgr
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		final android.net.NetworkInfo mobile = connMgr
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

		return wifi.isConnected() || mobile.isConnected();

	}

	/**
	 * Method to check if the device is connected using a 3G network.
	 * 
	 * @param context
	 * @return boolean
	 */
	public static boolean is3gConnection(Context context) {

		final ConnectivityManager connMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		final android.net.NetworkInfo mobile = connMgr
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

		return mobile.isConnected();

	}

}
